package com.hsj.goldstore.mvp.view.fragment;

import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.hsj.goldstore.R;
import com.hsj.goldstore.adapter.ViewpageChangeAdapter;
import com.hsj.goldstore.base.BaseMvpFrament;
import com.hsj.goldstore.base.PresenterFactory;
import com.hsj.goldstore.base.PresenterLoader;
import com.hsj.goldstore.mvp.present.fragment.GoldStorePresent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/25.
 */

public class GoodsFragment extends BaseMvpFrament<GoldStorePresent, GoldStoreView> implements GoldStoreView, ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener, OnItemClickListener {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_right)
    TextView btnRight;

    @BindView(R.id.rb_all)
    RadioButton rbAll;
    @BindView(R.id.rb_one)
    RadioButton rbOne;
    @BindView(R.id.rb_two)
    RadioButton rbTwo;
    @BindView(R.id.rg_goods_type)
    RadioGroup rgGoodsType;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    Unbinder unbinder;
    @BindView(R.id.cb_goods)
    ConvenientBanner cbGoods;
    private List<BaseMvpFrament> fagmentList;
    private ViewpageChangeAdapter adapter;
    private int currentPage = 0;

    @Override
    protected int getResouseId() {
        return R.layout.fragment_goods;
    }

    @Override
    protected void initView() {
        initTitle();
        initClick();
        initCarcousel();
    }

    private List<String> carouseList = new ArrayList<>();

    /**
     * 初始化轮播图
     */
    private void initCarcousel() {
        carouseList.add("");
        carouseList.add("");
        carouseList.add("");
        //轮播图
        cbGoods.setPages(new CBViewHolderCreator<CbImageHolderView>() {
            @Override
            public CbImageHolderView createHolder() {
                return new CbImageHolderView();
            }
        }, carouseList)
                //设置指示器是否可见
                .setPointViewVisible(true)
                //设置自动切换（同时设置了切换时间间隔）
                .startTurning(5000)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.mipmap.ic_dot, R.mipmap.ic_dot_2})
                //设置指示器的方向（左、中、右）
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                //设置点击监听事件
                .setOnItemClickListener(this)
                //设置手动影响（设置了该项无法手动切换）
                .setManualPageable(true);
    }

    private void initClick() {
        rgGoodsType.setOnCheckedChangeListener(this);
        viewpager.addOnPageChangeListener(this);
    }

    private void initTitle() {
        tvTitle.setText("金卖网加州店");
        btnRight.setText("搜索");
        btnRight.setVisibility(View.GONE);
        btnBack.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        fagmentList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            fagmentList.add(new GoodsTypeFagment(i));
        }
        adapter = new ViewpageChangeAdapter(getChildFragmentManager(), fagmentList);
        viewpager.setAdapter(adapter);

        viewpager.setCurrentItem(currentPage);
    }

    @Override
    public Loader<GoldStorePresent> onCreateLoader(int id, Bundle args) {
        return new PresenterLoader<>(getActivity(), new PresenterFactory<GoldStorePresent>() {
            @Override
            public GoldStorePresent create() {
                return new GoldStorePresent();
            }
        });
    }

    @Override
    public void showInfo() {

    }

    @Override
    public void callPhone(String phone) {

    }

    @Override
    public String getPhone() {
        return null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_right, R.id.cb_goods})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_right:
                break;
            case R.id.cb_goods:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurrentPage(position);
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setCurrentPage(int position) {
        currentPage = position;
        switch (currentPage) {
            case 0:
                rgGoodsType.check(R.id.rb_all);
                break;
            case 1:
                rgGoodsType.check(R.id.rb_one);
                break;
            case 2:
                rgGoodsType.check(R.id.rb_two);
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_all:
                currentPage = 0;
                viewpager.setCurrentItem(currentPage);
                break;
            case R.id.rb_one:
                currentPage = 1;
                viewpager.setCurrentItem(currentPage);
                break;
            case R.id.rb_two:
                currentPage = 2;
                viewpager.setCurrentItem(currentPage);

                break;
            default:
                break;
        }

    }

    @Override
    public void onItemClick(int position) {

    }
}
